import {
  doDelete, doDeleteObj,
  doGet, doPostListList, doPostParam, doPostToFormData,
  doPostToList,
  doPostToList1,
  doPostToObj,
  doPostParamAndObj,
  doPutToList,
  doPutToObj,
} from '@/services/http';
import { Reducer } from 'redux';

export interface HttpModelState {
  pageData: any;
  data: any;
}
export interface HttpModelType {
  namespace: string;
  state: HttpModelState;
  effects: any;
  reducers: {
    req: Reducer<HttpModelState>;
  };
  subscriptions: any;
}
const HttpModel: HttpModelType = {
  namespace: 'http',
  state: {
    pageData: {
      list: [],
      pagination: {},
    },
    data: {},
  },
  effects: {
    /**================  以下的方法只返回statusCode=0的response    ============================*/
    /**
     *   post请求分页查询
     *   优化处理： 如果分页查询url发生改变，那么会重置pageData
     */
    *postFetchPage({ payload, callback }, { call, put }) {
      const response = yield call(doPostToObj, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { pageData: response.data } });
        if (callback) callback(response);
        return response;
      }
    },
    /**
     *   get请求分页查询
     *   优化处理： 如果分页查询url发生改变，那么会重置pageData
     */
    *getFetchPage({ payload, callback }, { call, put }) {
      const response = yield call(doGet, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { pageData: response.data } });
        if (callback) callback(response);
        return response;
      }
    },

    /**
     * 
     * post请求分页查询
     * 同时传 params(pageNo, pageSize), body
     */
    *postFetchPageParamAndBody({ payload, callback }, { call, put }) {
      const response = yield call(doPostParamAndObj, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { pageData: response.data } });
        if (callback) callback(response);
        return response;
      }
    },

    /**
     *   get请求
     */
    *getData({ payload, callback }, { call, put }) {
      const response = yield call(doGet, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  post请求
     *  参数obj
     */
    *postObj({ payload, callback }, { call, put }) {
      const response = yield call(doPostToObj, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    *postFormData({ payload, callback }, { call, put }) {
      const response = yield call(doPostToFormData, payload.url, payload.formData);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  post请求
     *  参数data
     */
    *postData({ payload, callback }, { call, put }) {
      const response = yield call(doPostParam, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  post请求obj
     *  参数list
     */
    *postList({ payload, callback }, { call, put }) {
      const response = yield call(doPostToObj, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  post请求obj
     *  参数list
     */
    *postListList({ payload, callback }, { call, put }) {
      const response = yield call(doPostListList, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  put请求
     *  参数obj
     */
    *putObj({ payload, callback }, { call, put }) {
      const response = yield call(doPutToObj, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  put请求
     *  参数list
     */
    *putList({ payload, callback }, { call, put }) {
      const response = yield call(doPutToList, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     *  put请求
     *  参数list
     */
    *postList1({ payload, callback }, { call, put }) {
      const response = yield call(doPostToList1, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },
    /**
     * delete请求
     */
    *delData({ payload, callback }, { call, put }) {
      const response = yield call(doDelete, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },

    /* delete请求
     * 参数 Obj
     */
    *delObj({ payload, callback }, { call, put }) {
      const response = yield call(doDeleteObj, payload);
      if (response.statusCode === '0') {
        yield put({ type: 'req', payload: { data: response } });
        if (callback) callback(response);
      }
      return response;
    },

    /**================  以下的方法会直接返回response, 需要使用回调函数时使用    ============================*/

    /**
     *  get方法
     *  不论结果都使用回调
     */
    *getDataWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doGet, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },
    /**
     *  post方法
     *  不论结果都使用回调
     */
    *postDataWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doPostParam, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },

    /**
     *  post方法
     *  不论结果都使用回调，且不执行副作用
     */
    *postDataWithResNoPut({ payload, callback }, { call, put }) {
      const response = yield call(doPostParam, payload);
      callback(response);
      return response;
    },

    /**
     *  post方法
     *  参数obj
     *  不论结果都使用回调
     */
    *postObjWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doPostToObj, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },
    /**
     *  post方法
     *  参数list
     *  不论结果都使用回调
     */
    *postListWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doPostToList, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },
    /**
     *  delete
     *  方法不论结果都使用回调
     */
    *delDataWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doDelete, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },
    /**
     *  put方法
     *  参数obj
     *  不论结果都使用回调
     */
    *putObjWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doPutToObj, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },
    /**
     *  put方法
     *  参数list
     *  不论结果都使用回调
     */
    *putListWithRes({ payload, callback }, { call, put }) {
      const response = yield call(doPutToList, payload);
      yield put({ type: 'req', payload: { data: response } });
      callback(response);
      return response;
    },
    *initData({ payload, callback }, { call, put }) {
      yield put({
        type: 'req',
        payload: {
          pageData: {
            list: [],
            pagination: {},
          },
          data: {},
        },
      });
    },
  },

  reducers: {
    req(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        // console.log(pathname)
        if (pathname === '/user/login') {
          dispatch({ type: 'initData' });
        }
      })
    }
  }
};
export default HttpModel;
